package com.jianbing.shasha.service.impl;

import java.util.Collections;
import java.util.List;

import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.shasha.domain.ShashaStudyQues;
import com.jianbing.shasha.mapper.ShashaQuesMusicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.shasha.mapper.ShashaStudyMapper;
import com.jianbing.shasha.domain.ShashaStudy;
import com.jianbing.shasha.service.IShashaStudyService;

/**
 * 各关学习内容Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@Service
public class ShashaStudyServiceImpl implements IShashaStudyService 
{
    @Autowired
    private ShashaStudyMapper shashaStudyMapper;
    @Autowired
    private ShashaQuesMusicMapper shashaQuesMusicMapper;

    /**
     * 查询各关学习内容
     * 
     * @param id 各关学习内容主键
     * @return 各关学习内容
     */
    @Override
    public ShashaStudy selectShashaStudyById(Long id)
    {
        return shashaStudyMapper.selectShashaStudyById(id);
    }

    /**
     * 查询各关学习内容列表
     * 
     * @param shashaStudy 各关学习内容
     * @return 各关学习内容
     */
    @Override
    public List<ShashaStudy> selectShashaStudyList(ShashaStudy shashaStudy)
    {
        return shashaStudyMapper.selectShashaStudyList(shashaStudy);
    }

    /**
     * 新增各关学习内容
     * 
     * @param shashaStudy 各关学习内容
     * @return 结果
     */
    @Override
    public int insertShashaStudy(ShashaStudy shashaStudy)
    {
        return shashaStudyMapper.insertShashaStudy(shashaStudy);
    }

    /**
     * 修改各关学习内容
     * 
     * @param shashaStudy 各关学习内容
     * @return 结果
     */
    @Override
    public int updateShashaStudy(ShashaStudy shashaStudy)
    {
        return shashaStudyMapper.updateShashaStudy(shashaStudy);
    }

    /**
     * 批量删除各关学习内容
     * 
     * @param ids 需要删除的各关学习内容主键
     * @return 结果
     */
    @Override
    public int deleteShashaStudyByIds(Long[] ids)
    {
        return shashaStudyMapper.deleteShashaStudyByIds(ids);
    }

    /**
     * 删除各关学习内容信息
     * 
     * @param id 各关学习内容主键
     * @return 结果
     */
    @Override
    public int deleteShashaStudyById(Long id)
    {
        return shashaStudyMapper.deleteShashaStudyById(id);
    }

    /**
     * 获取指定大关的学习内容
     *
 * @param levelId
     * @return java.util.List<com.jianbing.shasha.domain.ShashaStudyQues>
     * @author zheng
     * @create 2024/6/15
     **/
    @Override
    public List<ShashaStudyQues> getStudyQuesByLevelId(Integer levelId) {
        List<ShashaStudyQues> shashaStudyQues = shashaStudyMapper.getStudyQuesByLevelId(levelId);
        Collections.sort(shashaStudyQues,(ques1,ques2)->{
            if(ques1.getVideoPath() !=null && ques2.getVideoPath()!=null){
                return 0;
            }else{
                return ques1.getVideoPath()==null?1:-1;
            }
        });
        String type =  shashaStudyQues.get(0).getEmotionType();
        List<String> musicPath = shashaQuesMusicMapper.getMusicByType(type);
        Collections.shuffle(musicPath);
        for(ShashaStudyQues s:shashaStudyQues){
            if(musicPath.size()!=0){
                s.setMusicPath(musicPath.get(0));
                musicPath.remove(0);
            }
            else{
                return null;
            }
        }
        return shashaStudyQues;
    }
}
